var websocket_page={};

websocket_page.action = {
    initBody: function () {
        websocket_page.action.openWebSocket("111");
        websocket_page.action.bindEvent();
    },
    bindEvent:function () {
        $("#sendBtn").click(function () {
            websocket_page.action.sendMsg("testMsg");
            return false;
        });
    },
    websocketInit : function(){
        //连接发生错误的回调方法
        websocket_page.data.websocket.onerror = function(){
            console.log("websocket连接发生异常");
        };

        //连接成功建立的回调方法
        websocket_page.data.websocket.onopen = function(event){
            console.log("websocket连接打开");
        };

        //接收到消息的回调方法
        websocket_page.data.websocket.onmessage = function(event){
			console.log("收到数据:"+event.data);
        };

        //连接关闭的回调方法
        websocket_page.data.websocket.onclose = function(){
            console.log("websocket连接关闭");
        };
    },

    //关闭websocket
    closeWebSocket : function(){
        if(websocket_page.data.websocket != null){
            websocket_page.data.websocket.close();
        }
    },

    //打开websocket连接
    openWebSocket : function(carId){
        //判断当前浏览器是否支持WebSocket
        if ('WebSocket' in window) {
            websocket_page.data.websocket = new WebSocket("ws://127.0.0.1:8080/cimrFrame_comm/websocket/"+carId);
            websocket_page.action.websocketInit();
        } else {
            alert('当前浏览器不支持webSocket！无法访问该设备的实时数据，请更换浏览器访问!');
        }
    },

    //发送信息
    sendMsg : function(msg){
        websocket_page.data.websocket.send(msg);
    }
};

websocket_page.data = {
    websocket:null
};

websocket_page.event={
    'isReaded':false,
    onReady: function () {
        websocket_page.action.initBody();
    }
};

AppComm.action.ready(function () {
    if (!websocket_page.event.isReaded) {
        websocket_page.event.isReaded = true;
        websocket_page.event.onReady();
    }
});